// 客户端交互协议
import { isApp, isAndroid, getAppVersoin, compareAppVersion } from './common.js';
import { 
    APP_INVOKE_PAGE,
    APP_HEAD_HIDE, APP_HEAD_SHOW, 
    APP_COME_BACK, 
    APP_LOGIN_PAGE, 
    APP_SERVICE_IM_NEW,
    APP_SHOP_CAR,
    NATIVE_ORDER_COMFIRM,
    APP_SETTING_RIGHT_BTN,
    APP_COMEBACK_CANCEL,
    APP_COMEBACK_JS_FUNC
} from '../constant/appAgreement.config.js';

import {
    IOS_SHOP_DAY_RECOMMEND, IOS_SHOP_DAY_SECKILL,
    ANDROID_SHOP_DAY_RECOMMEND, ANDROID_SHOP_DAY_SECKILL,
    ANDROID_BEAN_CODE_TASK, IOS_BEAN_CODE_TASK
} from '../constant/appAgreement.config.js';

/**
 * 打开原生页面
 * @param {*} url 
 */
const goNativePage = (url) => window.location.href = url;

/**
 * @desc 右上角按钮定义
 * @param {*} hide 
 */
const getHeadRightBtn = (rightText, url) => isApp() && goNativePage(APP_SETTING_RIGHT_BTN+`?rightText=${rightText}&rightLink=${encodeURIComponent(url)}`);

/**
 * 隐藏或显示APP头部
 * @param {boolean} hide 是否隐藏头部
 */
const appHideOrShowHead = (hide = true) => isApp() && goNativePage(hide ? APP_HEAD_HIDE : APP_HEAD_SHOW);

/**
 * 返回上一层
 */
const appGoBack = () => isApp() && goNativePage( APP_COME_BACK );

/**
 * 	当APP 内的 h5 点击头部的 返回按钮时 会触发js的回调
 */
const appComebackJSFunc = (func) => isApp() &&  goNativePage( APP_COMEBACK_JS_FUNC + func );

/**
 * 	取消返回按钮的全局注册事件
 */
const appComebackCancel = () => isApp() && goNativePage( APP_COMEBACK_CANCEL );

/**
 * 每日秒杀链接构建
 */
const buildNativeDaySeckill = () => APP_INVOKE_PAGE + (isAndroid() ? ANDROID_SHOP_DAY_SECKILL: IOS_SHOP_DAY_SECKILL);
/**
 * 每日推荐链接构建
 */
const buildNativeDayRecommend = () => APP_INVOKE_PAGE + (isAndroid() ? ANDROID_SHOP_DAY_RECOMMEND: IOS_SHOP_DAY_RECOMMEND);

/**
 * 打开登录页面
 */
const nativeToLogin = () => isApp() && goNativePage( APP_LOGIN_PAGE );

/**
 * 打开商城客服
 */
const nativeToShopServie = () => goNativePage( APP_SERVICE_IM_NEW );

/**
 * 打开购物车
 */
const nativeToShopCar = () => goNativePage( APP_SHOP_CAR );

/**
 * 打开绿豆任务页
 */
const nativeToBeanCodeTask = () => goNativePage( isAndroid() ? ANDROID_BEAN_CODE_TASK: IOS_BEAN_CODE_TASK );

/**
 * 打开订单详情页面
 */
const nativeToOrderComfirm = url => goNativePage( NATIVE_ORDER_COMFIRM + url );

export {
    buildNativeDaySeckill,
    buildNativeDayRecommend,
    appHideOrShowHead,
    appGoBack,
    getHeadRightBtn,
    appComebackCancel,
    appComebackJSFunc,

    nativeToLogin as appOpenLogin,
    nativeToShopServie as appOpenShopIm,
    nativeToShopCar as appOpenShopCar,
    nativeToBeanCodeTask as appBeanCodeTask,
    nativeToOrderComfirm as appOrderComfirm
};
